package com.sysmanagment.dao;

import javax.persistence.EntityManager;

import org.jboss.seam.annotations.In;

public abstract class DAOImpl<T> implements DAO<T> {
	@In(value="em",create=true)
	protected EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	@Override
	public T findById(T entity,Object id) {
		return (T) entityManager.find(entity.getClass(), id);
	}

	@Override
	public T merge(T entity) {
		return entityManager.merge(entity);
	}

	@Override
	public T persist(T entity) {
		entityManager.persist(entity);
		return entity;
	}

	@Override
	public void remove(T entity) {
		entityManager.remove(entity);
	}
	/**
	 * Obtains and returns the current hibernate session.
	 *
	 * @return the current hibernate session.
	 */
	protected EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * This method must return the entity's class the DAO handles.
	 * 
	 * @return The class the entity's DAO handles.
	 */
	protected abstract Class<T> getEntityClass();
	
	
	
}
